package com.moyun.zbmy.zizhou.ui.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.base.BaseActivity;
import com.moyun.zbmy.zizhou.base.BaseFragment;
import com.moyun.zbmy.zizhou.ui.adapter.holder.BaseAbsHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.CultureBannerHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.HomeHotVideoHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.HomeLiveHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.LiveHomeAdvanceHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.LiveHomeItemHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.LiveTvEntryHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.NewAdvertisementHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.NewSpecialAreaHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.NewSpecialHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.News24Holder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.OnlyTitleHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.SearchSubscriptionNoListHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.SubscriptionNoHomeTypeHolder;
import com.moyun.zbmy.zizhou.ui.adapter.holder.TvHolder;
import com.moyun.zbmy.zizhou.ui.util.IListShowData;
import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.LittleLifeManager;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;

import java.util.ArrayList;
import java.util.List;

/**
 * 新 通用适配器
 */
public class NewListAdapter extends BaseHolderAbsAdapter<IListShowData, BaseAbsHolder<IListShowData>> {

    public static final int VIEWTYPE_BANNER = 1;//轮播图
    public static final int VIEWTYPE_EXPRESS = 2;//快讯
    public static final int VIEWTYPE_LIVE = 3;//直播
    public static final int VIEWTYPE_POPULAR = 4;//热门视频
    public static final int VIEWTYPE_SPECIAL_AREA = 5;//专区（书记专区、县长专区）
    public static final int VIEWTYPE_NEWS = 6;//新闻item
    public static final int VIEWTYPE_ADVERTISEMENT = 7;//广告
    public static final int VIEWTYPE_LIVE_TITLE = 8;//直播标签（正在直播，直播回顾）
    public static final int VIEWTYPE_LIVE_ADVANCE = 9;//直播预约
    public static final int VIEWTYPE_LIVE_ITEM = 10;//直播Item
    public static final int VIEWTYPE_TV_ENTRY = 11;//电视、广播入口
    public static final int VIEWTYPE_SUBSCRIPTION_NO_TYPE = 12;//部门乡镇类型（订阅号类型）
    public static final int VIEWTYPE_SUBSCRIPTION_NO_TITLE = 13;//部门乡镇新闻tip（订阅号）
    public static final int VIEWTYPE_SUBSCRIPTION_NO_LIST = 14;//订阅号（部门乡镇搜索的订阅号列表）
    public static final int VIEWTYPE_SPECIAL_ITEM = 15;//新闻列表中的专题（同广告接口一起获取到的）
    private LittleLifeManager littleLifeManager;
    private OnItemInternalClick bannerActiveClick;
    private BaseActivity activity;
    private BaseFragment fragment;
    private ArrayList<Pair<String, String>> pairs;

    private OnItemInternalClick onLiveSubscribeClick;
    private LiveHomeItemHolder.LiveItemClickListener liveListener;
    private LiveTvEntryHolder.OnTvSelectListener tvEntryListener;
    private SubscriptionNoHomeTypeAdapter.OnHomeSubscriptionNoTypeClickListener subscriptionNoTypeClickListener;
    private SubscriptionNoAdapter.OnSubscriptionNoClickListener  subscriptionNoClickListener;
    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setFragment(BaseFragment fragment) {
        this.fragment = fragment;
    }

    public void setPairs(ArrayList<Pair<String, String>> pairs) {
        this.pairs = pairs;
    }

    private HomeLiveHolder.OnLivePlayingListener playingListener;

    public OnItemInternalClick getOnLiveSubscribeClick() {
        return onLiveSubscribeClick;
    }

    public void setOnLiveSubscribeClick(OnItemInternalClick onLiveSubscribeClick) {
        this.onLiveSubscribeClick = onLiveSubscribeClick;
    }

    public LiveHomeItemHolder.LiveItemClickListener getLiveListener() {
        return liveListener;
    }

    public void setLiveListener(LiveHomeItemHolder.LiveItemClickListener liveListener) {
        this.liveListener = liveListener;
    }

    public LiveTvEntryHolder.OnTvSelectListener getTvEntryListener() {
        return tvEntryListener;
    }

    public void setTvEntryListener(LiveTvEntryHolder.OnTvSelectListener tvEntryListener) {
        this.tvEntryListener = tvEntryListener;
    }

    public void setSubscriptionNoTypeClickListener(SubscriptionNoHomeTypeAdapter.OnHomeSubscriptionNoTypeClickListener subscriptionNoTypeClickListener) {
        this.subscriptionNoTypeClickListener = subscriptionNoTypeClickListener;
    }

    public void setSubscriptionNoClickListener(SubscriptionNoAdapter.OnSubscriptionNoClickListener subscriptionNoClickListener) {
        this.subscriptionNoClickListener = subscriptionNoClickListener;
    }

    public NewListAdapter(Context context, List list) {
        super(context, list);
        littleLifeManager = new LittleLifeManager();
    }


    @Override
    public BaseAbsHolder<IListShowData> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAbsHolder bah = null;
        View v = null;
        switch (viewType) {
            case VIEWTYPE_BANNER://轮播图
                v = LayoutInflater.from(context).inflate(R.layout.item_culture_banner, parent, false);
                bah = new CultureBannerHolder(context, v);
                break;
            case VIEWTYPE_NEWS://24小时新闻
                v = LayoutInflater.from(context).inflate(R.layout.item_news_24_cl, parent, false);
                bah = new News24Holder(context, v);
                break;
            case VIEWTYPE_LIVE://直播
                v = LayoutInflater.from(context).inflate(R.layout.item_home_live, parent, false);
                bah = new HomeLiveHolder(context, v, fragment, playingListener);
                break;
            case VIEWTYPE_POPULAR://热门视频
                v = LayoutInflater.from(context).inflate(R.layout.item_home_hot_video, parent, false);
                bah = new HomeHotVideoHolder(context, v);
                bah.setInternalClick(this);
                break;
            case VIEWTYPE_SPECIAL_AREA://专区
                v = LayoutInflater.from(context).inflate(R.layout.item_special_area, parent, false);
                bah = new NewSpecialAreaHolder(context, v);
                break;
            case VIEWTYPE_ADVERTISEMENT://广告
                v = LayoutInflater.from(context).inflate(R.layout.item_advertisement_normal, parent, false);
                bah = new NewAdvertisementHolder(context, v);
                break;
            case VIEWTYPE_LIVE_TITLE://标题
            case VIEWTYPE_SUBSCRIPTION_NO_TITLE://订阅号中的新闻 tip
                v = LayoutInflater.from(context).inflate(R.layout.item_live_title, parent, false);
                bah = new OnlyTitleHolder(context, v);
                break;
            case VIEWTYPE_LIVE_ITEM://直播Item(列表形式)
                v = LayoutInflater.from(context).inflate(R.layout.item_live_home_child, parent, false);
                bah = new LiveHomeItemHolder(context, v, liveListener, fragment);
                bah.setPairs(pairs);
                bah.setFragment(fragment);
                bah.setActivity(activity);
                break;
            case VIEWTYPE_LIVE_ADVANCE://直播预约
                v = LayoutInflater.from(context).inflate(R.layout.item_live_advance, parent, false);
                bah = new LiveHomeAdvanceHolder(context, v);
                bah.setInternalClick(onLiveSubscribeClick);
                break;
            case VIEWTYPE_TV_ENTRY://电视
                v = LayoutInflater.from(context).inflate(R.layout.live_tv_item, parent, false);
                bah = new LiveTvEntryHolder(context, v, tvEntryListener);
                break;
            case VIEWTYPE_SUBSCRIPTION_NO_TYPE://部门乡镇类型（订阅号类型）
                v = LayoutInflater.from(context).inflate(R.layout.item_subscripton_no_home_type, parent, false);
                bah = new SubscriptionNoHomeTypeHolder(context, v, subscriptionNoTypeClickListener);
                break;
            case VIEWTYPE_SUBSCRIPTION_NO_LIST:
                v = LayoutInflater.from(context).inflate(R.layout.item_subscription_no_gridview, parent, false);
                bah = new SearchSubscriptionNoListHolder(context, v, subscriptionNoClickListener);
                break;
            case VIEWTYPE_SPECIAL_ITEM:
                v = LayoutInflater.from(context).inflate(R.layout.item_special_normal, parent, false);
                bah = new NewSpecialHolder(context, v);
                break;
            default:
                v = LayoutInflater.from(context).inflate(R.layout.item_news_24_cl, parent, false);
                bah = new News24Holder(context, v);
                break;
        }
        bah.setActivity(activity);
        bah.setFragment(fragment);
        bah.setPairs(pairs);
        return bah;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(BaseAbsHolder<IListShowData> holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.setItemMode(position == getItemCount() - 1 ? BaseAbsHolder.ITEMMODE_LAST : BaseAbsHolder.ITEMMODE_NORMAL);
    }

    @Override
    public void onResume() {
        littleLifeManager.onResume();
    }

    @Override
    public void onPause() {
        littleLifeManager.onPause();
    }

    @Override
    public void onDestory() {
        littleLifeManager.onDestory();
    }


    @Override
    public NewListAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    @Override
    public void addDatas(List<IListShowData> list) {
        super.addDatas(list);
    }

    public void resetDatasFroms(int pos, List<? extends IListShowData> datas, boolean isForceAdd) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        if (pos < 0) {
            if (isForceAdd) {
                addDatass(datas);
            }
        } else {
            int count = getItemCount();
            if (pos < count) {
                data = data.subList(0, pos);
//                JLog.e("after sub size is " + data.size());
                data.addAll(datas);
                notifyItemRangeChanged(pos, Math.max(count - pos, datas.size()));
            } else {
                addDatass(datas);
            }
        }
    }

    public void addDatass(List<? extends IListShowData> list) {
        if (data == null) {
            data = new ArrayList<>();
            data.addAll(list);
            if (data.size() > 0) {
                notifyDataSetChanged();
            }
        } else if (list != null && list.size() > 0) {
            int fromPos = getItemCount();
            if (fromPos < 0) {
                fromPos = 0;
            }
            data.addAll(list);
            notifyItemRangeChanged(fromPos, list.size());
        }
    }

    public void setDatass(List<? extends IListShowData> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }


}

